package bancodedados;

import java.util.ArrayList;
import java.util.List;

import estruturas.Turma;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class TurmaDAO {
    private SQLiteDatabase db;
	private Helper helper;
	private String[] columns = {"numero","ano"};
	private long insertNumero;
	
	public TurmaDAO(Context context)
	{
		helper = new Helper(context);
	}
	
	public void open() throws SQLException
	{
		db = helper.getWritableDatabase();
	}
	
	public void close()
	{
		db.close();
	}

	public Turma addTurma(Turma t)
	{
		ContentValues values = new ContentValues();
		
		values.put("ano", t.getAno() );
		values.put("numero", t.getNumero());
		
		
		setInsertNumero( db.insert("cadastroTurma", null, values));
		Cursor cursor = db.query("Turma", columns, null, null, null, null, null, null);
		
		cursor.moveToFirst();
		Turma newTurma = cursorToTurma(cursor);
		cursor.close();
		
		return newTurma;
	}
	
	public List<String> getLista()
	{
		List<String> turma = new ArrayList<String>();
		
		Cursor cursor = db.query("Turma", columns, null, null,null,null, null, null);
		
		cursor.moveToFirst();
		while(!cursor.isAfterLast())
		{
			turma.add(cursorToTurma(cursor).getNumero());
			cursor.moveToNext();
		}
		
		cursor.close();
		return turma;
	}
	
	public boolean existeTurma(String n){
		Cursor cursor = db.query("Turm", columns, "Lower(numero) = '" + n.toLowerCase() + "'", null, null, null, null);
		
		if (cursor.getCount()>0){
			cursor.close();
			return true;
		}else{
			cursor.close();
			return false;
		}
		
	}
	
	
	private Turma cursorToTurma(Cursor cursor)
	{
		Turma turma = new Turma();
		
		
		turma.setNumero(cursor.getString(1));
		turma.setAno(cursor.getString(1));
		return turma;
	}

	public long getInsertNumero() {
		return insertNumero;
	}

	public void setInsertNumero(long insertNumero) {
		this.insertNumero = insertNumero;
	}
}

  

